public class Solution3 {

    public static void main(String[] args) {
        int[][] grid ={ {0,0,1,0,0,0,0,1,0,0,0,0,0},
                        {0,0,0,0,0,0,0,1,1,1,0,0,0},
                        {0,1,1,0,1,0,0,0,0,0,0,0,0},
                        {0,1,0,0,1,1,0,0,1,0,1,0,0},
                        {0,1,0,0,1,1,0,0,1,1,1,0,0},
                        {0,0,0,0,0,0,0,0,0,0,1,0,0},
                        {0,0,0,0,0,0,0,1,1,1,0,0,0},
                        {0,0,0,0,0,0,0,1,1,0,0,0,0}};
        int f = maxAreaOfIsland(grid);
        System.out.println(f);
    }

    public static int[] dx = {0, 0, 1, -1};
    public static int[] dy = {1, -1, 0, 0};
    public static int ret;
    public static int m;
    public static int n;
    public static boolean[][] visit;
    public static int num;

    public static  int maxAreaOfIsland(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        visit = new boolean[m][n];

        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(grid[i][j] == 1 && visit[i][j] == false) {
                    visit[i][j] = true;
                    num = 1;
                    dfs(grid, i, j);
                    if(num > ret) {
                        ret = num;
                    }
                }
            }
        }
        return ret;
    }

    public static void dfs(int[][] grid, int i, int j) {
        for(int k = 0; k < 4; k++) {
            int x = i + dx[k];
            int y = j + dy[k];
            if(x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 && visit[x][y] == false) {
                num++;
                visit[x][y] = true;
                dfs(grid, x, y);
            }
        }
    }
}
